<template>
  <div class="clue-pool-config">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      :label-width="labelWidth"
      v-loading="formLoading"
      class="config-form"
    >
      <div class="config-card">
        <!-- 操作 -->
        <div class="card-header">
          <div class="header-content">
            <div class="title-section">
              <h3 class="card-title">线索公海规则设置</h3>
            </div>
            <el-button
              type="primary"
              @click="onSubmit"
              v-hasPermi="['crm:module:update']"
              class="save-btn"
            >
              保存
            </el-button>
          </div>
        </div>

        <!-- 表单 -->
        <div class="card-body">
          <el-form-item label="线索公海规则设置" prop="enabled" class="form-item">
            <el-switch
              v-model="formData.enabled"
              class="custom-switch"
              inline-prompt
              style="--el-switch-on-color: #0052cc; --el-switch-off-color: #dcdfe6"
              active-text="启用"
              inactive-text="禁用"
              :active-value="true"
              :inactive-value="false"
            />
          </el-form-item>

          <div class="config-section">
            <el-form-item label="连续未跟进天数" class="form-item">
              <div class="input-with-tip">
                <el-input-number
                  v-model="formData.contactExpireDays"
                  class="custom-input-number"
                  :min="0"
                />
                <span class="input-tip">
                  如果线索连续N天内没有创建任何跟进记录，则会掉入公海，配置为0时此规则不生效
                </span>
              </div>
            </el-form-item>

            <el-form-item label="指定日期内未转化" class="form-item">
              <div class="input-with-tip">
                <el-input-number
                  v-model="formData.transExpireDays"
                  class="custom-input-number"
                  :min="0"
                />
                <span class="input-tip">
                  如果线索在指定期限内没有转化，则会掉入公海，配置为0时此规则不生效
                </span>
              </div>
            </el-form-item>

            <el-form-item label="是否需要提醒" prop="notifyEnabled" class="form-item">
              <el-radio-group
                v-model="formData.notifyEnabled"
                @change="changeNotifyEnable"
                class="custom-radio-group"
              >
                <el-radio :label="false" class="custom-radio">不提醒</el-radio>
                <el-radio :label="true" class="custom-radio">提醒</el-radio>
              </el-radio-group>
            </el-form-item>

            <div v-if="formData.notifyEnabled" class="notify-section">
              <el-form-item label="提前提醒天数" class="form-item">
                <div class="input-with-tip">
                  <el-input-number
                    :min="0"
                    v-model="formData.notifyDays"
                    class="custom-input-number"
                  />
                  <span class="input-tip"> 会在资源掉入公海之前提前通知给负责人 </span>
                </div>
              </el-form-item>
            </div>
          </div>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import * as CluePoolConfigApi from '@/api/crm/cluePool'
import { CardTitle } from '@/components/Card'

defineOptions({ name: 'CrmCustomerPoolConfig' })

const props = defineProps({
  moduleId: {
    type: Number
  }
})

const queryParams = reactive({
  moduleId: props.moduleId
})

// 响应式计算属性
const labelWidth = computed(() => {
  return window.innerWidth <= 768 ? '120px' : '160px'
})

watch(
  () => props.moduleId,
  (newVal) => {
    queryParams.moduleId = newVal
    getConfig()
  }
)

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const formLoading = ref(false)
const formData = ref({
  enabled: false,
  contactExpireDays: 0,
  transExpireDays: 0,
  notifyEnabled: false,
  notifyDays: 0
})
const formRules = reactive({
  enabled: [{ required: true, message: '是否启用线索公海不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 获取配置 */
const getConfig = async () => {
  try {
    formLoading.value = true
    const data = await CluePoolConfigApi.getCluePoolConfig(queryParams)
    if (data === null) {
      resetForm()
      return
    }
    formData.value = data
  } finally {
    formLoading.value = false
  }
}

/** 提交配置 */
const onSubmit = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    formData.value.moduleId = props.moduleId
    const data = formData.value
    await CluePoolConfigApi.saveCluePoolConfig(data)
    message.success(t('common.updateSuccess'))
    await getConfig()
    formLoading.value = false
  } finally {
    formLoading.value = false
  }
}

/** 更改客户公海规则设置 */
const changeEnable = () => {
  if (!formData.value.enabled) {
    formData.value.contactExpireDays = undefined
    formData.value.transExpireDays = undefined
    formData.value.notifyEnabled = false
    formData.value.notifyDays = undefined
  }
}

const resetForm = () => {
  formData.value.enabled = false
  formData.value.contactExpireDays = 0
  formData.value.transExpireDays = 0
  formData.value.notifyEnabled = false
  formData.value.notifyDays = 0
}

/** 更改提前提醒设置 */
const changeNotifyEnable = () => {
  if (!formData.value.notifyEnabled) {
    formData.value.notifyDays = undefined
  }
}

onMounted(() => {
  getConfig()
})
</script>

<style scoped>
.clue-pool-config {
  padding: 16px;
  background: #f8f9fa;
}

/* 表单样式 */
.config-form {
  max-width: 1200px;
  margin: 0 auto;
}

/* 卡片样式 */
.config-card {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

/* 卡片头部 */
.card-header {
  background: linear-gradient(135deg, #0052cc 0%, #003d99 100%);
  padding: 20px 24px;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.title-section {
  flex: 1;
}

.card-title {
  color: white;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.save-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  padding: 8px 24px;
  border-radius: 6px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.save-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

/* 卡片主体 */
.card-body {
  padding: 24px;
}

.form-item {
  margin-bottom: 24px;
}

.form-item :deep(.el-form-item__label) {
  color: #303133;
  font-weight: 600;
  line-height: 1.5;
}

/* 配置区域 */
.config-section {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  border: 1px solid #e4e7ed;
}

.notify-section {
  background: #f0f7ff;
  border-radius: 6px;
  padding: 16px;
  border: 1px solid #b3d8ff;
  margin-top: 16px;
}

/* 开关样式 */
.custom-switch {
  --el-switch-on-color: #0052cc;
  --el-switch-off-color: #dcdfe6;
}

/* 单选框组样式 */
.custom-radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.custom-radio {
  margin-right: 0;
  padding: 8px 16px;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  transition: all 0.3s ease;
  background: white;
}

.custom-radio:hover {
  border-color: #0052cc;
  background: #f0f7ff;
}

.custom-radio :deep(.el-radio__input.is-checked .el-radio__inner) {
  background-color: #0052cc;
  border-color: #0052cc;
}

.custom-radio :deep(.el-radio__input.is-checked + .el-radio__label) {
  color: #0052cc;
  font-weight: 600;
}

.custom-radio.is-checked {
  border-color: #0052cc;
  background: #f0f7ff;
}

/* 输入框样式 */
.input-with-tip {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.custom-input-number {
  width: 200px;
}

.custom-input-number :deep(.el-input__wrapper.is-focus) {
  border-color: #0052cc;
  box-shadow: 0 0 0 1px #0052cc inset;
}

.custom-input-number :deep(.el-input-number__decrease),
.custom-input-number :deep(.el-input-number__increase) {
  color: #0052cc;
}

.custom-input-number :deep(.el-input-number__decrease:hover),
.custom-input-number :deep(.el-input-number__increase:hover) {
  background: #f0f7ff;
  color: #003d99;
}

.input-tip {
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
  padding: 8px 12px;
  background: #f5f7fa;
  border-radius: 4px;
  border-left: 3px solid #0052cc;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .clue-pool-config {
    padding: 12px;
  }

  .card-header {
    padding: 16px 20px;
  }

  .header-content {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .save-btn {
    width: 100%;
    max-width: 200px;
  }

  .card-body {
    padding: 16px;
  }

  .config-section {
    padding: 16px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .custom-radio-group {
    flex-direction: column;
    gap: 8px;
  }

  .custom-radio {
    width: 100%;
    text-align: center;
  }

  .custom-input-number {
    width: 100%;
    max-width: 300px;
  }

  .input-with-tip {
    gap: 12px;
  }
}

@media (max-width: 480px) {
  .clue-pool-config {
    padding: 8px;
  }

  .card-header {
    padding: 12px 16px;
  }

  .card-title {
    font-size: 16px;
  }

  .card-body {
    padding: 12px;
  }

  .config-section {
    padding: 12px;
  }

  .notify-section {
    padding: 12px;
  }

  .custom-input-number {
    width: 100%;
  }
}

/* 加载状态优化 */
.el-loading-mask {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(2px);
}

/* 动画效果 */
.config-card {
  transition: all 0.3s ease;
}

.config-card:hover {
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.custom-radio,
.input-tip {
  transition: all 0.3s ease;
}

/* 主题色变量 */
:root {
  --primary-color: #0052cc;
  --primary-hover: #003d99;
  --primary-light: #f0f7ff;
  --border-color: #e4e7ed;
  --text-color: #303133;
  --bg-color: #f8f9fa;
}

/* Element Plus 组件样式覆盖 */
:deep(.el-form-item__content) {
  flex-wrap: wrap;
}

:deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
